//
//  SysMenu.m
//  cocos2dHelloWord
//
//  Created by 勤信为诚 on 11-9-24.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "SysMenu.h"
#import "UserData.h"

@implementation SysMenu

@synthesize ticketsThreadone;
@synthesize timer;
@synthesize label;
int countClick;


+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	SysMenu *layer = [SysMenu node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}


-(id) init
{
    countClick=0;
    self=[super init];
    if(self)
    {
        
		label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:18];
        
       
		// position the label on the center of the screen
		label.position =  ccp(100 , 10);
		
		// add the label as a child to this Layer
		[self addChild: label];
        
        /*
        CCSprite *sp=[CCSprite spriteWidhFile:@"Default.png"];
        sp.anchorPoint=CGPointZero;
        
        [self addChild:sp z:0 tag:1];*/
        
        [CCMenuItemFont setFontName:@"Marker Felt"];
        [CCMenuItemFont setFontSize:25];
        
        [CCMenuItemFont setFontSize:25];
        
        CCMenuItem *newGame=[CCMenuItemFont itemFromString:@"New Game" target:self selector:@selector(newGame:)];
        
        CCMenuItem *loadGame=[CCMenuItemFont itemFromString:@"Load Map" target:self selector:@selector(loadMap:)];
        
        CCMenuItem *gamesetting=[CCMenuItemFont itemFromString:@"Setting" target:self selector:nil];
        
        CCMenuItem *helpGame=[CCMenuItemFont itemFromString:@"Help" target:self selector:nil];
        CCMenuItem *quiteGame=[CCMenuItemFont itemFromString:@"Stop" target:self selector:@selector(stopTimer:)];
  
        CCMenuItem *calldata=[CCMenuItemFont itemFromString:@"Call Data" target:self selector:@selector(calldata:)];
        
        CCMenu *menu=[CCMenu menuWithItems:newGame,loadGame,gamesetting,helpGame,quiteGame,calldata, nil];
        [menu alignItemsVertically];
        [self addChild: menu z:1 tag:2];
    }
    return self;
}

-(void) loadMap:(id)sender
{
    [[CCDirector sharedDirector] replaceScene:[MapLayer node]];
   /* 
   CCTMXTiledMap* gamemap= [CCTMXTiledMap tiledMapWithTMXFile:@"isometric_grass_and_water.tmx"];
    [self addChild:gamemap UserData9];*/
}

-(void)calldata:(id)sender{
    UserData* currentDateClassObject = [[UserData alloc] init];
    
    SEL mySelector = @selector(stringForDate:usingFormatter:);
    NSMethodSignature * sig = [[UserData class] 
                               instanceMethodSignatureForSelector: mySelector];
    
    NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];
    [myInvocation setTarget: currentDateClassObject];
    [myInvocation setSelector: mySelector];
    
    NSDate * myDate = [NSDate date];
    [myInvocation setArgument: &myDate atIndex: 2];
    
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle: NSDateFormatterMediumStyle];    
    [myInvocation setArgument: &dateFormatter atIndex: 3];
    
    NSString * result = nil;    
    [myInvocation retainArguments];    
    [myInvocation invoke];
    [myInvocation getReturnValue: &result];
    NSLog(@"The result is: %@", result);
}

-(void) newGame:(id)sender
{
//    ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(runTimer) object:nil];
//    [ticketsThreadone setName:@"Thread-1"];  
//    [ticketsThreadone start]; 
    //[[CCDirector sharedDirector] replaceScene:[GameLayer node]];
    [self run];
}

-(void) runTimer{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
// timer=   [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(nexttime) userInfo:nil repeats:NO];
   [pool release];
}

-(void) run{
    NSLog(@"222");
    timer=[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(nexttime) userInfo:nil repeats:YES];
}

-(void) nexttime
{
    countClick++;
    int mmmsecond=countClick%1000;
    int second=(countClick/1000)%60;
    int min=(countClick/1000/60);
    
    [label setString:[NSString stringWithFormat:@"%02i:%02i.%03i",min,second,mmmsecond]];
     
    NSLog(@"%i:%i %i",min,second,mmmsecond);
}

-(void) stopTimer:(id)sender{
    if (timer!=nil) {
        [timer invalidate];
        //[timer release];
        //timer=nil;
    }
}
/*
-(void)dealloc{
    [timer release];
    [ticketsThreadone release];
    [label release];
    [super dealloc];
}*/
@end
